<template>
    <div class="header">
        <!-- 折叠按钮 -->
        <div class="collapse-btn" @click="collapseChage">
<!--            <i class="el-icon-menu"></i>-->
            <!--<i class="">-->
                <!--<img :src="require('../../assets/img/logo/logo.png')" alt="" style="width: 70px;">-->
            <!--</i>-->
        </div>


        <div class="header-right">
            <div class="header-user-con">
                <div class="logo">空气能设备管理平台</div>
                <!--如果屏幕过小采用抽屉式点击展开菜单-->
                <i v-if="isShowMenu" @click="drawer = true" type="primary" style="margin-left: 16px;flex: 1;" class="el-icon-menu"></i>
                <template v-if="isShowMenu">
                    <el-drawer
                            size="40%"
                            :visible.sync="drawer"
                            :direction="direction"
                            :withHeader="false"
                            :show-close="false">
                        <el-menu class="sidebar-el-menu" :default-active="onRoutes" background-color="#324157"
                                 text-color="#bfcbd9" active-text-color="#20a0ff" unique-opened router>
                            <template v-for="item in items">
                                <template v-if="item.subs">
                                    <el-submenu :index="item.index" :key="item.index" style="border-bottom: 1px solid #5c5c5c;">
                                        <template slot="title">
                                            <i class="" style="margin-right: 10px;"><img class="menu-img" :src="require('../../assets/img/menuIcon/'+item.icon)" alt=""></i><span slot="title">{{ item.title }}</span>
                                        </template>
                                        <template v-for="subItem in item.subs">
                                            <el-submenu v-if="subItem.subs" :index="subItem.index" :key="subItem.index" style="padding-left: 70px;">
                                                <template slot="title">{{ subItem.title }}</template>
                                                <el-menu-item v-for="(threeItem,i) in subItem.subs" :key="i" :index="threeItem.index">
                                                    {{ threeItem.title }}
                                                </el-menu-item>
                                            </el-submenu>
                                            <el-menu-item v-else :index="subItem.index" :key="subItem.index" style="padding-left: 70px;">
                                                {{ subItem.title }}
                                            </el-menu-item>
                                        </template>
                                    </el-submenu>
                                </template>
                                <template v-else>
                                    <el-menu-item :index="item.index" :key="item.index" style="border-bottom: 1px solid #5c5c5c;">
                                        <i class="" style="margin-right: 10px;"><img class="menu-img" :src="require('../../assets/img/menuIcon/'+item.icon)" alt=""></i> <span slot="title">{{ item.title }}</span>
                                    </el-menu-item>
                                </template>
                            </template>
                        </el-menu>
                    </el-drawer>
                </template>
                <!-- 全屏显示 -->
                <!--<div class="btn-fullscreen" @click="handleFullScreen">-->
                    <!--<el-tooltip effect="dark" :content="fullscreen?`取消全屏`:`全屏`" placement="bottom">-->
                        <!--<i class="el-icon-rank"></i>-->
                    <!--</el-tooltip>-->
                <!--</div>-->
                <!--否则按照列表展开菜单-->
                <template v-if="!isShowMenu">
                    <el-menu class="sidebar-el-menu" :default-active="onRoutes" mode="horizontal" background-color="#242f42" style="flex: 1"
                             text-color="#bfcbd9" active-text-color="#20a0ff" unique-opened router>
                        <template v-for="item in items">
                            <template v-if="item.subs">
                                <el-submenu :index="item.index" :key="item.index">
                                    <template slot="title">
                                        <i class="" style="margin-right: 5px;"><img class="menu-img" :src="require('../../assets/img/menuIcon/'+item.icon)" alt=""></i><span slot="title">{{ item.title }}</span>
                                    </template>
                                    <template v-for="subItem in item.subs">
                                        <el-submenu v-if="subItem.subs" :index="subItem.index" :key="subItem.index" >
                                            <template slot="title">{{ subItem.title }}</template>
                                            <el-menu-item v-for="(threeItem,i) in subItem.subs" :key="i" :index="threeItem.index">
                                                {{ threeItem.title }}
                                            </el-menu-item>
                                        </el-submenu>
                                        <el-menu-item v-else :index="subItem.index" :key="subItem.index">
                                            {{ subItem.title }}
                                        </el-menu-item>
                                    </template>
                                </el-submenu>
                            </template>
                            <template v-else>
                                <el-menu-item :index="item.index" :key="item.index">
                                    <i class="" style="margin-right: 5px;"><img class="menu-img" :src="require('../../assets/img/menuIcon/'+item.icon)" alt=""></i> <span slot="title">{{ item.title }}</span>
                                </el-menu-item>
                            </template>
                        </template>
                    </el-menu>
                </template>

                <!-- 消息中心 -->
                <!--<div class="btn-bell">-->
                    <!--<el-tooltip effect="dark" :content="message?`有${message}条未读消息`:`消息中心`" placement="bottom">-->
                        <!--<router-link to="/tabs">-->
                            <!--<i class="el-icon-bell"></i>-->
                        <!--</router-link>-->
                    <!--</el-tooltip>-->
                    <!--<span class="btn-bell-badge" v-if="message"></span>-->
                <!--</div>-->
                <!-- 用户头像 -->
                <div class="user-avator"><img src="../../assets/img/normal-user.png"></div>
                <!-- 用户名下拉菜单 -->
                <el-dropdown class="user-name" trigger="click" @command="handleCommand">
                    <span class="el-dropdown-link">
                        {{username}} <i class="el-icon-caret-bottom"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown">

                        <el-dropdown-item command="loginout">退出登录</el-dropdown-item>
                        <!--<el-dropdown-item divided command="daping">可视大屏</el-dropdown-item>-->
                        <el-dropdown-item divided command="fullScreen">{{fullscreen?`取消全屏`:`全屏`}}</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>
        </div>
    </div>
</template>
<script>
    import bus from '../common/bus';
    export default {
        data() {
            return {
                collapse: false,
                fullscreen: false,
                name: 'linxin',
                message: 2,
                items: [],
                drawer: false,
                direction: 'ttb',
                isShowMenu:document.body.clientWidth<1300?true:false
            }
        },
        created(){
            this.items = JSON.parse(localStorage.getItem('menu_item'))
        },
        computed:{
            username(){
                let username = localStorage.getItem('ms_username');
                return username ? username : this.name;
            },
            onRoutes(){
                this.drawer = false;
                return this.$route.path.replace('/','');
            }
        },

        methods:{
            // 用户名下拉菜单选择事件
            handleCommand(command) {
                if(command == 'loginout'){
                    localStorage.removeItem('ms_username')
                    this.$router.push('/login');
                }
                if(command == 'fullScreen'){
                    this.handleFullScreen();
                }
                if(command == 'daping'){
                    const newWin = this.$router.resolve({path: 'http://101.201.153.92:8888/index.html', params: {id:'' }});
                    window.open('http://101.201.153.92:8888/index.html','_blank');
                }
            },
            // 侧边栏折叠
            collapseChage(){
                this.collapse = !this.collapse;
                bus.$emit('collapse', this.collapse);
            },
            // 全屏事件
            handleFullScreen(){
                let element = document.documentElement;
                if (this.fullscreen) {
                    if (document.exitFullscreen) {
                        document.exitFullscreen();
                    } else if (document.webkitCancelFullScreen) {
                        document.webkitCancelFullScreen();
                    } else if (document.mozCancelFullScreen) {
                        document.mozCancelFullScreen();
                    } else if (document.msExitFullscreen) {
                        document.msExitFullscreen();
                    }
                } else {
                    if (element.requestFullscreen) {
                        element.requestFullscreen();
                    } else if (element.webkitRequestFullScreen) {
                        element.webkitRequestFullScreen();
                    } else if (element.mozRequestFullScreen) {
                        element.mozRequestFullScreen();
                    } else if (element.msRequestFullscreen) {
                        // IE11
                        element.msRequestFullscreen();
                    }
                }
                this.fullscreen = !this.fullscreen;
            }
        },
        mounted(){
            if(document.body.clientWidth < 1500){
                this.collapseChage();
            }
            //监听屏幕变化
            window.onresize = () => {
                bus.$emit('windowWidth', document.body.clientWidth);
                if(document.body.clientWidth<1300){
                    this.isShowMenu = true;
                }else{
                    this.isShowMenu = false;
                }
            }
        }
    }
</script>
<style scoped>
    .header {
        position: relative;
        box-sizing: border-box;
        width: 100%;
        height: 70px;
        font-size: 22px;
        color: #fff;
        /*padding-left: 262px;*/
    }
    .collapse-btn{
        float: left;
        padding: 0 21px;
        cursor: pointer;
        line-height: 70px;
    }
    .header .logo{
        float: left;
        /*width:250px;*/
        line-height: 70px;
        font-size: 20px;
        padding-left: 15px;
        margin-right: 2%;
        color: #FFFEFE;
    }
    .header-right{
        float: left;
        padding-right: 50px;
        width: 100%;
    }
    .header-user-con{
        display: flex;
        height: 70px;
        align-items: center;
    }
    .btn-fullscreen{
        transform: rotate(45deg);
        margin-right: 5px;
        font-size: 24px;
        left: 10px;
        top: 3px;
    }
    .btn-bell, .btn-fullscreen{
        position: relative;
        width: 30px;
        height: 30px;
        text-align: center;
        border-radius: 15px;
        cursor: pointer;
    }
    .btn-bell-badge{
        position: absolute;
        right: 0;
        top: -2px;
        width: 8px;
        height: 8px;
        border-radius: 4px;
        background: #f56c6c;
        color: #fff;
    }
    .btn-bell .el-icon-bell{
        color: #fff;
    }
    .user-name{
        margin-left: 10px;
        margin-right: 20px;
    }
    .user-avator{
        margin-left: 4%;
    }
    .user-avator img{
        display: block;
        width:40px;
        height:40px;
        border-radius: 50%;
    }
    .el-dropdown-link{
        color: #fff;
        cursor: pointer;
    }
    .el-dropdown-menu__item{
        text-align: center;
    }
   /deep/ .el-menu.el-menu--horizontal,.el-menu--horizontal>.el-menu-item{
       border:0;
   }
    /deep/ .header-user-con i img{
        height: 14px;
        width: 14px;
        vertical-align: initial;
        position: relative;
        top: 2px;
    }
    /deep/ .el-submenu__title,.el-menu-item{
        color: #fff !important;
    }
    /deep/ .el-drawer.ttb{
        overflow: unset;
        background: transparent;
    }
</style>
